// import { userLogin } from '../services/user';

import {
  getZone, getModel,
  getCode, findPicMany, createPic, deletePic
} from '../services/global';
import {exportExcel} from "../services/finance";

export default {
  namespace: 'global',
  state: {},
  effects: {

    /**
     * 获取编码
     * @param payload
     * @param call
     * @returns {IterableIterator<*>}
     */* getCode({payload}, {call}) {

      const response = yield call(getCode, payload);

      if (response) {
        const {result, status, message} = response;
        return result;
      }

    },

    * getModel({payload}, {call, put}) {
      const response = yield call(getModel, payload);
      const {table} = payload;
      if (response) {
        const {result} = response;
        const json = {};
        json[`${table}Model`] = result;
        yield put({
          type: 'onDataLoad',
          payload: json
        });
      }
    },


    /**
     * 获取行政区域
     * @param payload
     * @param call
     * @returns {IterableIterator<*>}
     */* getZone({}, {call}) {
      const response = yield call(getZone);
      return response;
    },
    * createPic({payload}, {call, put}) {
      const response = yield call(createPic, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "添加图片失败，请检查网络！", type: 'error'}
      });
    },
    * deletePic({payload}, {call, put}) {
      const response = yield call(deletePic, payload);

      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "更新失败，请检查网络！", type: 'error'}
      });
    },
    * findPicMany({payload}, {call, put}) {
      const response = yield call(findPicMany, payload);

      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {pics: result}
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response.result;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "图片获取失败，请检查网络！", type: 'error'}
      });
    }

  },

  reducers: {
    onDataLoad(state, {payload}) {
      state._notify = undefined;
      return {...state, ...payload};
    }

  }
};
